################################################################################
#####   Replication File for "Inadvertent Expansion in World Politics"   #######
################################################################################

# Analysis run using R version 4.2.1

rm(list=ls())

# Load required packages
require(foreign)
require(sandwich)
require(stargazer)
require(plyr)

# input great power territorial expansion data hard drive location below and load data
data<-read.csv("INPUT HARD DRIVE LOCATION HERE/Inadvertent_expansion_data_v1.csv")



##################################################
###  Tables, Figures, and Analysis in Article  ###
##################################################



### Table 1: Cases of Inadvertent Expansion

# subset cases of inadvertent expansion
Table1<-subset(data,inadvertent==1)

# keep only relevant information
Table1<-cbind(Table1[4],Table1[2],Table1[3],Table1[7])



### Figure 2: The Telegraph, Risk, & Inadvertent Expansion ###

# compute proportion of expansion that is inadvertent with & without the telegraph
mean(data$inadvertent[data$telegraph==1],na.rm=T) # 0.07633588
mean(data$inadvertent[data$telegraph==0],na.rm=T) # 0.375

# conduct statistical significance test using OLS regression with robust standard errors for telegraph
lm1<-lm(inadvertent~telegraph,data=data)
robse1<-sqrt(diag(vcovCL(lm1,type="HC1",cluster=~gainer_ccode)))

# robust standard error for the telegraph
robse1[2] # 0.05848605 

# 95% confidence interval for the telegraph
robse1[2]*1.96 # 0.1146327

# compute proportion of expansion that is inadvertent with & without risk
mean(data$inadvertent[data$risky==1],na.rm=T) # 0.09090909
mean(data$inadvertent[data$risky==0],na.rm=T) # 0.3191489

# conduct statistical significance test using OLS regression with robust standard errors for risk
lm2<-lm(inadvertent~risky,data=data)
robse2<-sqrt(diag(vcovCL(lm2,type="HC1",cluster=~gainer_ccode)))

# robust standard error for risk
robse2[2] # 0.04636979

# 95% confidence interval for risk
robse2[2]*1.96 # 0.0908848

# the actual figure was constructed using this data in Microsoft Excel



###################################################
###  Tables, Figures, and Analysis in Appendix  ###
###################################################



### Table A1: Summary Statistics for Inadvertent Expansion Data

# use stargazer package to generate summary statistics table for relevant variables
stargazer(data,type="text",omit=c("id","gainer_ccode","procedure","entity_ccode","loser_ccode","tc2018","tc_number"))



### Table A2: Inadvertent Expansion by Great Power

# first, separate intentional and inadvertent observations
dataInadv<-subset(data,inadvertent==1)
dataIntent<-subset(data,inadvertent==0)

# then, begin to manually create table, starting with actor column
TableA2<-as.data.frame(c("France","UK","Russia","Japan","Germany","USA","Austria","Italy","China"))
names(TableA2)<-"actor"

# add inadvertent expansion observations column
TableA2<-cbind(TableA2,c(nrow(subset(dataInadv,gainer_ccode==220)),
                           nrow(subset(dataInadv,gainer_ccode==200)),
                           nrow(subset(dataInadv,gainer_ccode==365)),
                           nrow(subset(dataInadv,gainer_ccode==740)),
                           nrow(subset(dataInadv,gainer_ccode==255)),
                           nrow(subset(dataInadv,gainer_ccode==2)),
                           nrow(subset(dataInadv,gainer_ccode==300)),
                           nrow(subset(dataInadv,gainer_ccode==325)),
                           nrow(subset(dataInadv,gainer_ccode==710))))
colnames(TableA2)[2]<-"inadvertent"

# add total expansion observations column
TableA2<-cbind(TableA2,c(nrow(subset(dataInadv,gainer_ccode==220))+nrow(subset(dataIntent,gainer_ccode==220)),
                           nrow(subset(dataInadv,gainer_ccode==200))+nrow(subset(dataIntent,gainer_ccode==200)),
                           nrow(subset(dataInadv,gainer_ccode==365))+nrow(subset(dataIntent,gainer_ccode==365)),
                           nrow(subset(dataInadv,gainer_ccode==740))+nrow(subset(dataIntent,gainer_ccode==740)),
                           nrow(subset(dataInadv,gainer_ccode==255))+nrow(subset(dataIntent,gainer_ccode==255)),
                           nrow(subset(dataInadv,gainer_ccode==2))+nrow(subset(dataIntent,gainer_ccode==2)),
                           nrow(subset(dataInadv,gainer_ccode==300))+nrow(subset(dataIntent,gainer_ccode==300)),
                           nrow(subset(dataInadv,gainer_ccode==325))+nrow(subset(dataIntent,gainer_ccode==325)),
                           nrow(subset(dataInadv,gainer_ccode==710))+nrow(subset(dataIntent,gainer_ccode==710))))
colnames(TableA2)[3]<-"total"                     

# add rate of inadvertent expansion column
TableA2<-cbind(TableA2,c(round(mean(data$inadvertent[data$gainer_ccode==220],na.rm=T),3),
                           round(mean(data$inadvertent[data$gainer_ccode==200],na.rm=T),3),
                           round(mean(data$inadvertent[data$gainer_ccode==365],na.rm=T),3),
                           round(mean(data$inadvertent[data$gainer_ccode==740],na.rm=T),3),
                           round(mean(data$inadvertent[data$gainer_ccode==255],na.rm=T),3),
                           round(mean(data$inadvertent[data$gainer_ccode==2],na.rm=T),3),
                           round(mean(data$inadvertent[data$gainer_ccode==300],na.rm=T),3),
                           round(mean(data$inadvertent[data$gainer_ccode==325],na.rm=T),3),
                           round(mean(data$inadvertent[data$gainer_ccode==710],na.rm=T),3)))
colnames(TableA2)[4]<-"rate"

# reorder the table by rate
TableA2<-TableA2[order(-TableA2$rate),]

# create a bottom row with totals to be appended to table
TableA2a<-as.data.frame(cbind("Total",sum(TableA2$inadvertent)))
TableA2a<-as.data.frame(cbind("Total",sum(TableA2$inadvertent),sum(TableA2$total)))
TableA2a<-as.data.frame(cbind("Total",sum(TableA2$inadvertent),sum(TableA2$total),round(sum(TableA2$inadvertent)/sum(TableA2$total),3)))
names(TableA2a)<-c("actor","inadvertent","total","rate")

# append bottom row to table
TableA2<-rbind(TableA2,TableA2a)

# drop extra bottom row and rename table variables
rm(TableA2a)
names(TableA2)<-c("Actor","Inadvertent","Total","Percent")



### Table A3: Inadvertent Expansion by Region

# manually create table starting with region column
TableA3<-as.data.frame(c("South & Central Asia","Sub-Saharan Africa","Asia-Pacific",
                          "Western Hemisphere","Middle East & North Africa","Europe"))
names(TableA3)<-"Region"

# then inadvertent expansion observations column
TableA3<-cbind(TableA3,c(nrow(subset(dataInadv,region==5)),
                           nrow(subset(dataInadv,region==3)),
                           nrow(subset(dataInadv,region==6)),
                           nrow(subset(dataInadv,region==1)),
                           nrow(subset(dataInadv,region==4)),
                           nrow(subset(dataInadv,region==2))))
colnames(TableA3)[2]<-"Inadvertent"

# then total expansion observations column
TableA3<-cbind(TableA3,c(nrow(subset(dataInadv,region==5))+nrow(subset(dataIntent,region==5)),
                           nrow(subset(dataInadv,region==3))+nrow(subset(dataIntent,region==3)),
                           nrow(subset(dataInadv,region==6))+nrow(subset(dataIntent,region==6)),
                           nrow(subset(dataInadv,region==1))+nrow(subset(dataIntent,region==1)),
                           nrow(subset(dataInadv,region==4))+nrow(subset(dataIntent,region==4)),
                           nrow(subset(dataInadv,region==2))+nrow(subset(dataIntent,region==2))))
colnames(TableA3)[3]<-"Total"  

# then rate of inadvertent expansion column
TableA3<-cbind(TableA3,round(TableA3[2]/TableA3[3],3))
colnames(TableA3)[4]<-"Percent"
TableA3<-TableA3[order(-TableA3$Percent),]



### Figure A1: Intentional & Inadvertent Expansion by Year, 1816-1945

# start by creating base data for the year
FigureA1<-as.data.frame(1816:2014)
names(FigureA1)<-"Year"

# use plyr package to count year observations for intentional and inadvertent expansion
dataInadv2<-count(dataInadv$year)
names(dataInadv2)<-c("Year","Inadvertent")
dataIntent2<-count(dataIntent$year)
names(dataIntent2)<-c("Year","Intentional")

# use plyr package to merge observations with year base data
FigureA1<-join(FigureA1,dataInadv2)
FigureA1<-join(FigureA1,dataIntent2)

# remove extra data
rm(dataInadv2,dataIntent2)

# sub NAs for zero in data
FigureA1[is.na(FigureA1)]<-0

# this data was used to create the figure in Microsoft Excel



### Table A4: Statistical Significance Tests for Telegraph, Risk, & Inadvertent Expansion

# use stargazer package to create statistical significance test results table
stargazer(lm1, lm2, type="text",se=list(robse1,robse2),omit="Constant")



### Table A5: Linear Probability Analysis of Inadvertent Expansion

# conduct full linear probability analysis and create table
lm3<-lm(inadvertent~telegraph+risky+year+distance+conflict+info_capacity+democracy+gainer_cinc,data=data)
robse3<-sqrt(diag(vcovCL(lm3,type="HC1",cluster=~gainer_ccode)))
stargazer(lm3, type="text",se=list(robse3),omit="Constant")



### Table A6: Logistic Regression Analysis of Inadvertent Expansion

# conduct full logistic regression analysis and create table
glmr1<-glm(inadvertent~telegraph+risky+year+distance+conflict+info_capacity+democracy+gainer_cinc,data=data, family=binomial(link="logit"))
robseglmr1<-sqrt(diag(vcovCL(glmr1,type="HC2",cluster=~gainer_ccode)))
stargazer(glmr1,type="text",se=list(robseglmr1))



### Table A7: Linear Probability Analysis of Inadvertent Expansion (extra_regional)

# conduct linear probability analysis with extra_regional control and create table
lm4<-lm(inadvertent~telegraph+risky+year+extra_regional+conflict+info_capacity+democracy+gainer_cinc,data=data)
robse4<-sqrt(diag(vcovCL(lm4,type="HC1",cluster=~gainer_ccode)))
stargazer(lm4, type="text",se=list(robse4),omit="Constant")



### Table A8: Linear Probability Analysis of Inadvertent Expansion (regime type measures) ###

# conduct linear probability analyses with alternative regime type controls and create table
lm5<-lm(inadvertent~telegraph+risky+year+distance+conflict+info_capacity+autocracy+gainer_cinc,data=data)
robse5<-sqrt(diag(vcovCL(lm5,type="HC1",cluster=~gainer_ccode)))

lm6<-lm(inadvertent~telegraph+risky+year+distance+conflict+info_capacity+polity+gainer_cinc,data=data)
robse6<-sqrt(diag(vcovCL(lm6,type="HC1",cluster=~gainer_ccode)))

lm7<-lm(inadvertent~telegraph+risky+year+distance+conflict+info_capacity+vdem_polyarchy+gainer_cinc,data=data)
robse7<-sqrt(diag(vcovCL(lm7,type="HC1",cluster=~gainer_ccode)))

stargazer(lm5,lm6,lm7,type="text",se=list(robse5,robse6,robse7),omit="Constant")


### Table A9: Linear Probability Analysis of Inadvertent Expansion (Dropping Great Powers)

# conduct linear probability analyses dropping great power actors one-by-one and create tables

# without the U.S.
lm8<-lm(inadvertent~telegraph+risky+year+distance+conflict+info_capacity+democracy+gainer_cinc,data=subset(data,gainer_ccode!=2))
robse8<-sqrt(diag(vcovCL(lm8,type="HC1",cluster=~gainer_ccode)))

# without the UK
lm9<-lm(inadvertent~telegraph+risky+year+distance+conflict+info_capacity+democracy+gainer_cinc,data=subset(data,gainer_ccode!=200))
robse9<-sqrt(diag(vcovCL(lm9,type="HC1",cluster=~gainer_ccode)))

# without France
lm10<-lm(inadvertent~telegraph+risky+year+distance+conflict+info_capacity+democracy+gainer_cinc,data=subset(data,gainer_ccode!=220))
robse10<-sqrt(diag(vcovCL(lm10,type="HC1",cluster=~gainer_ccode)))

# without Germany
lm11<-lm(inadvertent~telegraph+risky+year+distance+conflict+info_capacity+democracy+gainer_cinc,data=subset(data,gainer_ccode!=255))
robse11<-sqrt(diag(vcovCL(lm11,type="HC1",cluster=~gainer_ccode)))

# without Austria-Hungary
lm12<-lm(inadvertent~telegraph+risky+year+distance+conflict+info_capacity+democracy+gainer_cinc,data=subset(data,gainer_ccode!=300))
robse12<-sqrt(diag(vcovCL(lm12,type="HC1",cluster=~gainer_ccode)))

stargazer(lm8,lm9,lm10,lm11,lm12,type="text",se=list(robse8,robse9,robse10,robse11,robse12),omit="Constant")



### Table A10: Linear Probability Analysis of Inadvertent Expansion (Dropping Great Powers)

# conduct linear probability analyses dropping great power actors one-by-one and create table

# without Italy
lm13<-lm(inadvertent~telegraph+risky+year+distance+conflict+info_capacity+democracy+gainer_cinc,data=subset(data,gainer_ccode!=325))
robse13<-sqrt(diag(vcovCL(lm13,type="HC1",cluster=~gainer_ccode)))

# without Russia
lm14<-lm(inadvertent~telegraph+risky+year+distance+conflict+info_capacity+democracy+gainer_cinc,data=subset(data,gainer_ccode!=365))
robse14<-sqrt(diag(vcovCL(lm14,type="HC1",cluster=~gainer_ccode)))

# without China
lm15<-lm(inadvertent~telegraph+risky+year+distance+conflict+info_capacity+democracy+gainer_cinc,data=subset(data,gainer_ccode!=710))
robse15<-sqrt(diag(vcovCL(lm15,type="HC1",cluster=~gainer_ccode)))

# without Japan
lm16<-lm(inadvertent~telegraph+risky+year+distance+conflict+info_capacity+democracy+gainer_cinc,data=subset(data,gainer_ccode!=740))
robse16<-sqrt(diag(vcovCL(lm16,type="HC1",cluster=~gainer_ccode)))

stargazer(lm13,lm14,lm15,lm16, type="text",se=list(robse13,robse14,robse15,robse16),omit="Constant")



### Table A11: Linear Probability Analysis of Inadvertent Expansion (Dropping Regions)

# conduct linear probability analyses dropping regions one-by-one and create table

# Without the Western Hemisphere
lm17<-lm(inadvertent~telegraph+risky+year+distance+conflict+info_capacity+democracy+gainer_cinc,data=subset(data,region!=1))
robse17<-sqrt(diag(vcovCL(lm17,type="HC1",cluster=~gainer_ccode)))

# Without Europe
lm18<-lm(inadvertent~telegraph+risky+year+distance+conflict+info_capacity+democracy+gainer_cinc,data=subset(data,region!=2))
robse18<-sqrt(diag(vcovCL(lm18,type="HC1",cluster=~gainer_ccode)))

# Without Sub-Saharan Africa
lm19<-lm(inadvertent~telegraph+risky+year+distance+conflict+info_capacity+democracy+gainer_cinc,data=subset(data,region!=3))
robse19<-sqrt(diag(vcovCL(lm19,type="HC1",cluster=~gainer_ccode)))

# Without the Middle East & North Africa
lm20<-lm(inadvertent~telegraph+risky+year+distance+conflict+info_capacity+democracy+gainer_cinc,data=subset(data,region!=4))
robse20<-sqrt(diag(vcovCL(lm20,type="HC1",cluster=~gainer_ccode)))

# Without South & Central Asia 
lm21<-lm(inadvertent~telegraph+risky+year+distance+conflict+info_capacity+democracy+gainer_cinc,data=subset(data,region!=5))
robse21<-sqrt(diag(vcovCL(lm21,type="HC1",cluster=~gainer_ccode)))

# Without the Asia-Pacific
lm22<-lm(inadvertent~telegraph+risky+year+distance+conflict+info_capacity+democracy+gainer_cinc,data=subset(data,region!=6))
robse22<-sqrt(diag(vcovCL(lm22,type="HC1",cluster=~gainer_ccode)))

stargazer(lm17,lm18,lm19,lm20,lm21,lm22, type="text",se=list(robse17,robse18,robse19,robse20,robse21,robse22),omit="Constant")
